package cn.kzgg.pet.service.impl;

import cn.kzgg.org.domain.Employee;
import cn.kzgg.org.mapper.EmployeeMapper;
import cn.kzgg.pet.domain.Pet;
import cn.kzgg.pet.domain.PetDetail;
import cn.kzgg.pet.domain.PetOnlineAuditLog;
import cn.kzgg.pet.mapper.PetDetailMapper;
import cn.kzgg.pet.mapper.PetMapper;
import cn.kzgg.pet.mapper.PetOnlineAuditLogMapper;
import cn.kzgg.pet.mapper.PetTypeMapper;
import cn.kzgg.pet.service.IPetService;
import cn.kzgg.basic.service.impl.BaseServiceImpl;
import cn.kzgg.user.domain.Logininfo;
import jdk.nashorn.internal.runtime.regexp.joni.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author KZGG
 * @since 2022-06-25
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    public void update(Pet pet, Logininfo logininfo) {


    }

    /**
     * 覆写save方法，将店铺信息和细节保存
     * @param pet
     * @param logininfo
     */
    @Override
    public void save(Pet pet, Logininfo logininfo) {
        //获取登录员工
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        //设置对应的店铺
        pet.setShopId(employee.getShop_id());
        petMapper.save(pet);
        //获取前端传回来的细节
        PetDetail petDetail = pet.getDetail();
        //判断有没有
        if(petDetail!=null){
            //有细节就保存
            petDetail.setPetId(pet.getId());
           petDetailMapper.save(petDetail);
        }


    }

    /**
     * 宠物上架
     * @param ids
     */
    @Override
    public void onsale(List<Long> ids) {
        //审核宠物
        for (Long id : ids) {
            //拿到这个宠物
            Pet pet = petMapper.loadById(id);
            //审核名字
            StringBuilder sb = new StringBuilder();
            //审核不通过，记录日志，下一个继续
            if(sb.toString().length()>1){
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                log.setNote(sb.toString());
                petOnlineAuditLogMapper.save(log);
            }
            //2.通过修改状态和时间
            Map<String, Object> map = new HashMap<>();
            map.put("id",id);
            map.put("onSaleTime",new Date());
            petMapper.onsale(map);


        }


    }

    /**
     * 宠物下架
     * @param ids
     */
    @Override
    public void offsale(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("offSaleTime",new Date());
        petMapper.offSale(map);

    }
}
